<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="head">
      <div class="tit">
        <div class="tit1">Neal.fun</div>
      </div>
      <div class="title">
        <img src="https://neal.fun/spend/billgates.jpg" alt="" />
        花光比尔盖茨的钱
      </div>
      <div class="money">$100,000,000,000</div>
      <ul class="list">
        <!-- <li>
          <div class="item">
            <img src="img/3.jpg" alt="" />
            <div class="name">Big Mac</div>
            <div class="cost">$2</div>
            <div class="controls">
              <button class="sell" disabled>Sell</button>
              <input type="text" class="input" readonly />
              <button class="buy">Buy</button>
            </div>
          </div>
        </li> -->
      </ul>
    </div>

    <div class="spree-wrapper">
      <div class="spree-headline">你的方案</div>
      <div class="spree-items">
        <div class="spree-item-name">巨无霸</div>
        <div class="spree-item-amount">x2</div>
        <div class="spree-item-cost">$4</div>
      </div>

      <div class="spree-total">
        <span>总计</span>
        <div class="spree-total-money">$2,120,000,004</div>
      </div>
    </div>
<script type="module">
import {ajax} from './ajax.js'
const own = 100_000_000_000 // 现有的钱
const money = document.querySelector('.money')
const list = document.querySelector('.list')
let items = null
const cartItems = []
function total(){                                                                           
  return cartItems.reduce((s,item)=>{
    return s+item.count*item.cost
  },0)
}
function updateMoney(){
  money.innText=oen-total()
}
function updateCartItemsList(){

}
function itemHTML(item, index) {
  return `
  <li>
    <div class="item" data-index='${index}'>
      <img src="${item.img}" alt="" />
      <div class="name">${item.name}</div>
      <div class="cost">$${item.cost}</div>
      <div class="controls">
        <button class="sell" disabled  data-index='${index}'>卖</button>
        <input type="text" value="0" class="input" readonly  data-index='${index}'/>
        <button class="buy" data-index='${index}'>买</button>
      </div>
    </div>
  </li>`
}

ajax('./data.csv', false)
  .then(text => text.split('\n'))
  .then(lines => lines.map(line => {
    const [name, cost, img] = line.split(',')
    return {
      name: name.trim(),
      cost: +cost,
      img: img.trim()
    }
  }))
  .then(_items => {
    list.innerHTML = _items.map(itemHTML).join('')
    items = _items
  })

function updateItem(item) {
  let input = document
    .querySelector(`input[data-index="${item.index}"]`)
  let sell = document
    .querySelector(`button.sell[data-index="${item.index}"]`)
  input.value = item.count
  if(item.count > 0) {
    sell.removeAttribute('disabled')
  } else {
    sell.setAttribute('disabled', false)
  }
}

list.onclick = (e) => {
  if (e.target.tagName === 'BUTTON') {
    if (e.target.classList.contains('sell')) {
      // 卖
      const index =
        e.target.getAttribute('data-index')
      const find = cartItems.find(item =>
        item.index === index
      )
      find.count -= 1
      updateItem(find)
      updateMoney()
     
      console.log(cartItems)
    }
    if (e.target.classList.contains('buy')) {
      // 买
      const index =
        e.target.getAttribute('data-index')
      let find = cartItems.find(item =>
        item.index === index
      )
      if(find) {
        find.count += 1
      } else {
        find = {
          index,
          name: items[index].name,
          cost: items[index].cost,
          count: 1
        }
        cartItems.push(find)
      }
      updateItem(find)
      total()
      console.log(cartItems)
    }
  }
}

</script>
  </body>
</html>
